//
//  UICollectionView+FooterManager.m
//  CsdnPlus
//
//  Created by 孟瑞瑞 on 2020/4/21.
//  Copyright © 2020 CSDN. All rights reserved.
//

#import "UICollectionView+FooterManager.h"



@implementation UICollectionView (FooterManager)
+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class targetClass = [self class];
        SEL originalSelector = @selector(reloadData);
        SEL swizzledSelector = @selector(sy_reloadData);
        sy_swizzleMethod(targetClass, originalSelector, swizzledSelector);
    });
}

void sy_swizzleMethod(Class class, SEL originalSelector, SEL swizzledSelector) {
    Method originalMethod = class_getInstanceMethod(class, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
    
    IMP swizzledImp = method_getImplementation(swizzledMethod);
    char *swizzledTypes = (char *)method_getTypeEncoding(swizzledMethod);
    
    IMP originalImp = method_getImplementation(originalMethod);
    char *originalTypes = (char *)method_getTypeEncoding(originalMethod);
    
    BOOL success = class_addMethod(class, originalSelector, swizzledImp, swizzledTypes);
    if (success) {
        class_replaceMethod(class, swizzledSelector, originalImp, originalTypes);
    }else {
        // 添加失败，表明已经有这个方法，直接交换
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}


- (void)sy_reloadData {
    [self sy_reloadData];
    self.oldHeightOfContentSize = self.contentSize.height;
    dispatch_async(dispatch_get_main_queue(), ^{
        if (self.autoHideMjFooter) {
            [self setFooterShow];
        }
    });
}

#pragma mark - 添加属性
static const char *oldHeightOfContentSizeKey = "oldHeightOfContentSize";
- (void)setOldHeightOfContentSize:(CGFloat)oldHeightOfContentSize {
    objc_setAssociatedObject(self, oldHeightOfContentSizeKey, @(oldHeightOfContentSize), OBJC_ASSOCIATION_RETAIN);
}

- (CGFloat)oldHeightOfContentSize{
    return [objc_getAssociatedObject(self, oldHeightOfContentSizeKey) floatValue];
}

#pragma mark - 添加属性
static const char *autoHideMjFooterKey = "autoHideMjFooter";
- (void)setAutoHideMjFooter:(BOOL)autoHideMjFooter {
    objc_setAssociatedObject(self, autoHideMjFooterKey, @(autoHideMjFooter), OBJC_ASSOCIATION_RETAIN);
}

- (BOOL)autoHideMjFooter{
    return [objc_getAssociatedObject(self, autoHideMjFooterKey) boolValue];
}

/**
 数据不满一页的话就自动隐藏下面的“上拉加载更多”或是"没有更多数据" 。
 */
- (void)setFooterShow {
    dispatch_async(dispatch_get_main_queue(), ^{
        CGFloat heightOfContentSize = self.contentSize.height; // 内容高度
        // 计算tableView实际显示范围
        // 先拿到tableView实际的contentInset (因为下拉刷新时mj会重设contentInset)
        UIEdgeInsets originContentInset = self.mj_header.scrollViewOriginalInset;
        CGFloat actualHeight = self.frame.size.height - originContentInset.top - originContentInset.bottom;
        // 修正footer对contenInset的影响
        if (!self.mj_footer.hidden) { // 没有隐藏
            actualHeight = actualHeight + self.mj_footer.frame.size.height + 10; // 默认的mj_footer高度为44  默认实际偏移了54
        }
        if (heightOfContentSize == self.oldHeightOfContentSize) {
            [self.mj_footer endRefreshingWithNoMoreData];
            [self.mj_footer resetNoMoreData];
        }
        if (actualHeight >= heightOfContentSize) { // 实际显示高度大于内容高度 代表第一页都没有占满
            self.mj_footer.hidden = YES;
        } else {
            self.mj_footer.hidden = NO;
        }
    });
    
}
@end
